/**
 * 
 */
package kr.pe.johnkim.dayve.provider;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author johnkim
 *
 */
public class DayveItemEventDatabaseHandler {

	public static final String TAG = DayveItemEventDatabaseHandler.class.getSimpleName();
	
	Context ctx;
	DayveItemEventDatabaseHelper helper;
	SQLiteDatabase db;
	
	public DayveItemEventDatabaseHandler(Context ctx) {
		this.ctx = ctx;
		
		helper = new DayveItemEventDatabaseHelper(ctx);		
		
		db = helper.getWritableDatabase();
	}
	
	/**
	 * Database open 
	 */	
    public static DayveItemEventDatabaseHandler open(Context ctx) {
    	return new DayveItemEventDatabaseHandler(ctx);
    }
    
    /**
     * Transaction 을 정의 한다.
     */
	public void beginTransaction() {db.beginTransaction();}
	
	public void setTransactionSuccessful() {db.setTransactionSuccessful();}
	
	public void endTransaction() {db.endTransaction();}

	public SQLiteDatabase getSQLiteDatabase() {
		return this.db;
	}
	
	/**
	 * Database close 
	 * */
	public void close() {
		if(db.isOpen()) db.close();
	}
	
	// Item 테이블 컬럼 목록.
		private static final String[] DAYVE_ITEM_EVENT = new String[] {
			"_id", 
			"id_myhistory_item_events",
			"comment", 
			"amount", 
			"open_item_event_yn",
			"favorite_yn",
			"activate_yn",
			"modify_date",
			"register_date",
			"myhistory_members_id", 
			"myhistory_items_id_myhistory_items"
		};
		
		/**
		 * 아이템 이벤트 목록을 가져 온다.
		 * 
		 * 등록된 날짜의 역순으로 가져 온다.
		 * 
		 * @param _id
		 * @param limit
		 * @return
		 */
		public ArrayList<DayveItemEvent> selectItemEventList(String itemId) {
			Log.i(TAG, "selectItemEventList");
			Log.e(TAG, "query itemId : " + itemId);
			
			ArrayList<DayveItemEvent> itemEventList = new ArrayList<DayveItemEvent>();
			
			Cursor cursor = db.query(
					"dayve_item_event", 
					DAYVE_ITEM_EVENT, 
					"myhistory_items_id_myhistory_items=?", 
					new String[] {itemId}, 
					null, 
					null, 
					" register_date DESC ", 
					null
					);
			
			DayveItemEvent dayveItemEvent = null;
			
			while(cursor.moveToNext()) {
				dayveItemEvent = new DayveItemEvent();
				
				dayveItemEvent.set_id(cursor.getString(0));
				dayveItemEvent.setIdMyHistoryItemEvents(cursor.getString(1));
				dayveItemEvent.setComment(cursor.getString(2));
				dayveItemEvent.setAmount(cursor.getString(3));
				dayveItemEvent.setOpenItemEventYn(cursor.getString(4));
				dayveItemEvent.setFavoriteYn(cursor.getString(5));
				dayveItemEvent.setActivateYn(cursor.getString(6));
				dayveItemEvent.setModifyDate(cursor.getString(7));
				dayveItemEvent.setRegisterDate(cursor.getString(8));
				dayveItemEvent.setMyHistoryMembersId(cursor.getString(9));
				dayveItemEvent.setMyHistoryItemsIdMyHistoryItems(cursor.getString(10));
				
				itemEventList.add(dayveItemEvent);
			}
			
			cursor.close();
			
			return itemEventList;
		}
		
		/**
		 * 해당 category 의 item 목록을 가져 온다.
		 * 
		 * @param categoryId
		 * @return
		 */
//		public ArrayList<DayveItem> selectItemListForCategory(String categoryId) {
//			Log.i(TAG, "selectItemListForCategory");
//			
//			ArrayList<DayveItem> itemList = new ArrayList<DayveItem>();
//			
//			Cursor cursor = db.query(
//					"dayve_item", 
//					DAYVE_ITEM_EVENT, 
//					"myhistory_items_id_myhistory_items=?", 
//					new String[] {categoryId}, 
//					null, 
//					null, 
//					" name ASC ", 
//					null
//					);
//			
//			DayveItem dayveItem = null;
//			
//			while(cursor.moveToNext()) {
//				dayveItem = new DayveItem();
//				
//				dayveItem.set_id(cursor.getString(0));
//				dayveItem.setIdMyHistoryItems(cursor.getString(1));
//				dayveItem.setComment(cursor.getString(3));
//				dayveItem.setAmount(cursor.getString(4));
//				dayveItem.setOpenItemYn(cursor.getString(5));
//				dayveItem.setFavoriteYn(cursor.getString(6));
//				dayveItem.setActivateYn(cursor.getString(7));
//				dayveItem.setModifyDate(cursor.getString(8));
//				dayveItem.setRegisterDate(cursor.getString(9));
//				dayveItem.setMyHistoryMembersId(cursor.getString(10));
//				dayveItem.setMyHistoryCategoriesIdMyHistoryCategories(cursor.getString(11));
//				
//				itemList.add(dayveItem);
//			}
//			
//			cursor.close();
//			
//			return itemList;
//		}
		
		/**
		 * 아이템을 저장 한다.
		 * 
		 * @param values
		 * @return
		 */
		public Long insertDayveItemEvent(ContentValues values) {
//			Log.i(TAG, "insertDayveItemEvent");
			return db.insert("dayve_item_event", "", values);
		}
		
		/**
		 * 아이템을 삭제 한다.
		 * 
		 * @return
		 */
		public int deleteDayveItemEvent() {
			Log.i(TAG, "deleteDayveItemEvent");
			return db.delete("dayve_item_event", null, null);
		}
}
